package com.yidiandao.business.utils;

import android.content.Context;
import android.content.SharedPreferences;
import android.provider.Settings;
import android.telephony.TelephonyManager;

import java.io.UnsupportedEncodingException;
import java.util.UUID;

/**
 * 获取设备唯一标识工具
 *
 * <p>Created by ChenTao(chentao7v@gmail.com) on 2017/2/8 15:25
 */
public class DeviceUuidFactory {

    protected static final String PREFS_FILE = "device_id.xml";

    protected static final String PREFS_DEVICE_ID = "device_id";

    protected static UUID uuid;

    public DeviceUuidFactory(Context context) {

        if (uuid == null) {

            synchronized (DeviceUuidFactory.class) {
                if (uuid == null) {

                    final SharedPreferences prefs = context.getSharedPreferences(PREFS_FILE, 0);

                    final String id = prefs.getString(PREFS_DEVICE_ID, null);

                    if (id != null) {

                        // Use theidspreviously computed and stored in theprefsfile

                        uuid = UUID.fromString(id);

                    } else {

                        final String androidId =
                                Settings.Secure.getString(
                                        context.getContentResolver(), Settings.Secure.ANDROID_ID);

                        // Use the Android ID unless it's broken, in which casefallbackon deviceId,

                        // unless it's not available, thenfallbackon a random number which we store

                        // to aprefsfile

                        try {

                            if (!"9774d56d682e549c".equals(androidId)) {

                                uuid = UUID.nameUUIDFromBytes(androidId.getBytes("utf8"));

                            } else {

                                final String deviceId =
                                        ((TelephonyManager)
                                                context.getSystemService(
                                                        Context.TELEPHONY_SERVICE))
                                                .getDeviceId();

                                uuid =
                                        deviceId != null
                                                ? UUID.nameUUIDFromBytes(deviceId.getBytes("utf8"))
                                                : UUID.randomUUID();
                            }

                        } catch (UnsupportedEncodingException e) {

                            throw new RuntimeException(e);
                        }

                        // Write the value out to theprefsfile
                        prefs.edit().putString(PREFS_DEVICE_ID, uuid.toString()).commit();
                    }
                }
            }
        }
    }

    /**
     * Returns a unique UUID for the current android device.  As with all UUIDs, this unique ID is
     * "very highly likely"
     *
     * <p>
     *
     * <p>to be unique across all Android devices.  Much more so than ANDROID_ID is.
     *
     * <p>
     *
     * <p>
     *
     * <p>
     *
     * <p>
     *
     * <p>
     *
     * <p>The UUID is generated by using ANDROID_ID as the base key if appropriate, falling onBack
     * on
     *
     * <p>
     *
     * <p>TelephonyManager.getDeviceID() if ANDROID_ID is known to be incorrect, and finally
     * falling
     * onBack
     *
     * <p>
     *
     * <p>on a random UUID that's persisted to SharedPreferences if getDeviceID() does not return a
     *
     * <p>
     *
     * <p>usable value.
     *
     * <p>
     *
     * <p>
     *
     * <p>
     *
     * <p>
     *
     * <p>
     *
     * <p>In some rare circumstances, this ID may change.  In particular, if the device is factory
     * reset a new device ID
     *
     * <p>
     *
     * <p>may be generated.  In addition, if a user upgrades their phone from certain buggy
     * implementations of Android 2.2
     *
     * <p>
     *
     * <p>to a newer, non-buggy version of Android, the device ID may change.  Or, if a
     * useruninstallsyourappon
     *
     * <p>
     *
     * <p>a device that has neither a proper Android ID nor a Device ID, this ID may change
     * onreinstallation.
     *
     * <p>
     *
     * <p>
     *
     * <p>
     *
     * <p>
     *
     * <p>
     *
     * <p>Note that if the code falls onBack on using TelephonyManager.getDeviceId(), the resulting
     * ID will NOT
     *
     * <p>
     *
     * <p>change after a factory reset.  Something to be aware of.
     *
     * <p>
     *
     * <p>
     *
     * <p>
     *
     * <p>
     *
     * <p>
     *
     * <p>Works around a bug in Android 2.2 for many devices when using ANDROID_ID directly.
     *
     * @return UUID that may be used to uniquely identify your device for most purposes.
     * @see http://code.google.com/p/android/issues/detail?id=10603
     */
    public UUID getDeviceUuid() {
        return uuid;
    }
}
